<?php
/**
 * @file specialcolumn.php
 * @author LinLin
 * @date 2017年3月16日 上午10:26:25
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\beauty;

use \haibao\cms\business\control\ArticleController as ArticleController;
use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;

class SpecialColumnController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        //$this->savaLog();
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $articleList = array();
        if($controlData['manual']){
            $articleList = $this->getActualData($controlData['manual']);
        }
        $this->assign('articleList', $articleList);
        $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    public function getActualData($dataCondition){
        $tagId = 11004;
        if($dataCondition['tagId']){
            $tagId = explode(',',$dataCondition['tagId']);
        }
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(6);
        $articleEntity->setLimit($dataCondition['limit']);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(false);
        $articleEntity->setIsConditionTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        $articleEntity->setTagIds($tagId);
        $articleEntity->setOrderByNew(true,'desc');
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
        $this->parseTagId($dataCondition['tagId'],$dataCondition['tagId']);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        $articleList = array();
        foreach($mergeData as $key=>$article){
            $articleArr = $article->toArray();
            $articleArr['SiteType'] = $article->SiteType;
    
            if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){
                 
                $tag = $articles['tag'];
                $articleTag = $articles['articleTag'];
    
                $articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
                $articleArr['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
                $articleArr['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
                $articleArr['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
                 
                $articleArr['ProImage'] =  isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
                (isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
    
            }else{
                 
                $articleArr['PublishId'] = $article->Id;
                $tag = $imageColls['tag'];
                $collTag = $imageColls['collTag'];
                 
                $articleArr['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
                $articleArr['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
                $articleArr['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
                $articleArr['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
                $articleArr['ProImage'] =  isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR]:
                (isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }
    
            array_push($articleList,$articleArr);
        }
        return $articleList;
    }
} 